Skip to content

builtins: Allow numbers.range_step to use Decimal step size. - #172

Open
philipaconrad wants to merge 1 commit into
open-policy-agent:mainfrom
philipaconrad:philip/decimal-range-step
Open

builtins: Allow numbers.range_step to use Decimal step size.#172
philipaconrad wants to merge 1 commit into
open-policy-agent:mainfrom
philipaconrad:philip/decimal-range-step

Conversation

@philipaconrad

@philipaconrad philipaconrad commented Jun 23, 2026

Copy link
Copy Markdown
Member

What code changed, and why?

This PR extends the numbers.range_step builtin to support Decimal step sizes.

OPA uses bignums from the Go stdlib to support large values for the range and step sizes, but we do not have nice bignum support in Swift at the moment.

While the solution here is not as comprehensive as bignums, it does increase the number of valid cases we can support from OPA, and will fix a broken test case in our Compliance test suite.

How to test

  • Run OPA_COMPLIANCE_TESTS='numbersrange' make test-compliance
  • Observe that it finally passes now without errors! 🙂

Related Resources

This commit extends the `numbers.range_step` builtin to support
`Decimal` step sizes. This is not as comprehensive a solution as
bignum support, but it does increase the number of valid cases we
can support from OPA, and will fix a broken test case in our
Compliance test suite.

Signed-off-by: Philip Conrad <philip_conrad@apple.com>
@philipaconrad
philipaconrad requested a review from DFrenkel June 23, 2026 23:22
@DFrenkel

Copy link
Copy Markdown
Collaborator

🚀! I noticed that the same numbersrangestep/test-numbersrangestep.json: numbersrangestep/big int step shows as both passed and failed in compliance tests diffs. Do we possibly have another case of sync vs async issue?

/// Whether this Decimal represents a whole number (no fractional part), regardless of
/// magnitude. Unlike ``safeInt64Value`` this does not require the value to fit in `Int64`,
/// so very large integers such as `99999999999999999999` still report `true`.
public var isWholeNumber: Bool {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we cover this in Tests/ASTTests/DecimalExtensionsTests.swift

@philipaconrad philipaconrad Jun 24, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could add some test for that. 🤔 I'll work that into my next round of changes for the PR.


/// Whether this number is a whole number (no fractional part), regardless of magnitude.
/// Unlike ``int64Value`` this returns true even when the value is too large for `Int64`.
public var isInteger: Bool {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a series of tests that verify integerValue in Tests/ASTTests/RegoValueTests.swift. I would suggest adding unit tests there to cover isInteger and isPositive there

/// result holds at most a handful of elements. We iterate in Decimal space (arbitrary
/// precision) to avoid Int64 overflow while remaining correct for steps that are only
/// slightly larger than Int64.max.
private static func rangeWithDecimalStep(intA: Int64, intB: Int64, step: Decimal) -> RegoValue {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we extend rangeStepTests in Tests/RegoTests/BuiltinTests/NumbersTests.swift with validations for of these big steps or do you think compliance tests are sufficient?

@philipaconrad

philipaconrad commented Jun 24, 2026

Copy link
Copy Markdown
Member Author

I noticed that the same numbersrangestep/test-numbersrangestep.json: numbersrangestep/big int step shows as both passed and failed in compliance tests diffs. Do we possibly have another case of sync vs async issue?

Ah! So the red - diff under the "Failed" category indicates it's no longer part of the failing set from main. The green + diff under the "Passed" category indicates we've added that compliance test to the passing set on the PR.

No sync/async issues, just a confusingly-worded diff summary. I'll see about improving the readability of that message in a future PR. 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants